﻿using MiniFox.Configurations;
using MiniFox.Net.Configurations;
using MiniFox.Net.Ftp;
using MiniFox.Net.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Net
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class WebAdapterContainer
    {
        private HttpAdapterCollection _httpAdapters;
        private FtpAdapterCollection _ftpAdapters;

        static WebAdapterContainer()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        protected WebAdapterContainer()
        {
            _httpAdapters = new HttpAdapterCollection();
            _ftpAdapters = new FtpAdapterCollection();
            Initialize();
        }
        /// <summary>
        /// 
        /// </summary>
        protected virtual HttpAdapterCollection HttpAdapters
        {
            get { return this._httpAdapters; }
        }
        /// <summary>
        /// 
        /// </summary>
        protected virtual FtpAdapterCollection FtpAdapters
        {
            get { return this._ftpAdapters; }
        }
        /// <summary>
        /// 
        /// </summary>
        protected abstract void Initialize();
        #region 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public virtual HttpAdapter GetHttpAdapterByName(string name)
        {
            return this.HttpAdapters[name];
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public virtual FtpAdapter GetFtpAdapterByName(string name)
        {
            return this.FtpAdapters[name];
        }

        #endregion


    }

    internal class DefaultWebAdapterContainer : WebAdapterContainer
    {
        /// <summary>
        /// 
        /// </summary>
        internal static WebAdapterContainerConfiguration ContainerConfiguration
        {
            get
            {
                return ConfigurationFactory.GetConfig<WebAdapterContainerConfiguration>();
            }
        }

        protected override void Initialize()
        {
            foreach(HttpAdapterSetting setting in ContainerConfiguration.HttpAdapters)
            {
                HttpAdapter adapter = new HttpAdapter(setting);
                this.HttpAdapters.Add(adapter);
            }
            foreach (FtpAdapterSetting setting in ContainerConfiguration.FtpAdapters)
            {
                FtpAdapter adapter = new FtpAdapter(setting);
                this.FtpAdapters.Add(adapter);
            }
        }

    }
}
